package org.ping.qq.Login;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.ping.Tool.GetData;
import org.ping.Tool.LogUtil;

public class SetDate {
	private SetDate() {}
	
	private static SetDate set = new SetDate();
	
	public static SetDate getDate() {
		return set;
	}
	//往数据库添加用户
	public void addUser(String user,String openid) {
		Connection conn = GetData.getInstance().getConnection();
		Date da = new Date();
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		try {
			PreparedStatement stat = conn.prepareStatement("insert user(openid,username,date) values(?,?,?)");
			stat.setString(1, openid);
			stat.setString(2, user);
			stat.setString(3, format.format(da));
			stat.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			LogUtil.getLogUtil(this).showlog("添加用户出现了异常！");
			e.printStackTrace();
		}finally {
			try {
				conn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	//判断数据库有没有这个用户
	public boolean selectUser(String openid) {
		Connection conn = GetData.getInstance().getConnection();
		boolean emp = false;
		try {
			PreparedStatement stat = conn.prepareStatement("select count(*) from user where openid=?");
			stat.setString(1, openid);
			ResultSet res = stat.executeQuery();
			int num = 1;
			while (res.next()) {
				num = res.getInt(1);
			}
			if(num==0) {
				emp = true;
			}
		} catch (SQLException e) {
			LogUtil.getLogUtil(this).showlog("查询用户出现了异常！");
			e.printStackTrace();
		}finally {
			try {
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return emp;
	}
	
}
